package com.situ.mall.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.vo.ProductExcelVO;
import com.situ.mall.pojo.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

import java.util.Map;

//@Configuration
//@Controller
public class ProductExcelListener extends AnalysisEventListener<ProductExcelVO> {

    //@Autowired
    private ProductMapper productMapper;

    public ProductExcelListener(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }


    //一行一行读取excel中内容，每次读取一行调用invoke
    @Override
    public void invoke(ProductExcelVO data, AnalysisContext context) {
        System.out.println(data);
        Product product = new Product();
        BeanUtils.copyProperties(data, product);
        //避免和原有数据id冲突
        product.setId(null);
        productMapper.insert(product);
    }

    //读取表头数据
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头数据：" + headMap);
    }

    //所有数据读取完之后执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("ExcelListener.doAfterAllAnalysed");
    }
}
